<?php
 Class IndentAction extends Action{
 	public function index(){
 		$db=M();
 		Import('ORG.Util.Page');
 		$sql1="select count(*) tot from indent ";
 		$re=$db->query($sql1);
 		$count=$re[0]['tot'];
 		$page=new Page($count,10);
 		$show=$page->show();
 		$sql2="select i.*,u.username,sta.name status,t.* ,s.name shop_name,s.price price,s.img img from indent i 
 						left join user u on i.user_id=u.id
 						left join status sta on i.status_id=sta.id
 						left join touch t on  i.touch_id=t.id
 						left join shop s on  i.shop_id=s.id
 						group by i.code
 						limit {$page->firstRow},{$page->listRows}";
 		$rows=$db->query($sql2);
 		// echo '<pre>';
 		// print_r($rows);exit;
 		$this->assign('show',$show);
 		$this->assign('rows',$rows);
 		$this->display();
 	}
 	// 添加模版
 	public function add(){
 		$this->display();
 	}
 	// 添加操作
 	public function add_handle(){
 		$Indent=M('Indent');
 		if($Indent->create()){
 			$Indent->add();
 			$this->redirect('index');
 		}else{
 			$this->error('添加失败');
 		};
 	}

 	// 删除
 	public function delete(){
 		$Indent=M('Indent');
 		// 获取$.get传过来的id
 		$id=$_GET['id'];
		$re=$Indent->delete($id);
		if($re){
			$arr['status']=1;
			$arr['info']='删除成功';
			echo json_encode($arr);
		}
 		
 		
 	}


 	// 修改模版
 	public function edit(){
 		$Indent=M('Indent');
 		$code=$_GET['code'];
 		$row=$Indent->where('code='.$code)->find();

 		$status=M('Status');
 		$statusRows=$status->select();

 		$this->assign('row',$row);
 		$this->assign('statusRows',$statusRows);
 		$this->display();
 	}
 	// 修改操作
 	public function edit_handle(){
 		$Indent=M('Indent');
 		$Indent->where('code='.$_POST['code'])->save($_POST);
 		$this->redirect('index');
 	}

 	// 订单详情
 	public function shopList(){
 		$db=M();
 		$code=$_GET['code'];
 		$sql="select i.*,u.username,sta.name status,t.* ,s.name shop_name,s.price price,s.img img from indent i 
 						left join user u on i.user_id=u.id
 						left join status sta on i.status_id=sta.id
 						left join touch t on  i.touch_id=t.id
 						left join shop s on  i.shop_id=s.id
 						where code={$code}";
 		$rows=$db->query($sql);
 		foreach ($rows as $k => $v) {
 			// $total=$total+$v['price']*$v['number'];
 			$total+=$v['price']*$v['number'];
 		}
 		$this->assign('rows',$rows);
 		$this->assign('total',$total);
 		$this->display();
 	}
 }
?>